/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.DateUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.utils.excel.ExportExcel;
import com.kaaction.modules.ka.entity.SalesReturn;
import com.kaaction.modules.ka.entity.Statistics;
import com.kaaction.modules.ka.service.SalesReturnService;

/**
 * 退货管理Controller
 * @author huanghaiming
 * @version 2017-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/salesReturn")
public class SalesReturnController extends BaseController {

	@Autowired
	private SalesReturnService salesReturnService;
	
	@ModelAttribute
	public SalesReturn get(@RequestParam(required=false) String id) {
		SalesReturn entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = salesReturnService.get(id);
		}
		if (entity == null){
			entity = new SalesReturn();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:salesReturn:view")
	@RequestMapping(value = {"list", ""})
	public String list(SalesReturn salesReturn, HttpServletRequest request, HttpServletResponse response, Model model) {
		Statistics statistics =salesReturnService.selStatistics(salesReturn);
		model.addAttribute("statistics", statistics);
		Page<SalesReturn> page = salesReturnService.findPage(new Page<SalesReturn>(request, response), salesReturn); 
		model.addAttribute("page", page);
		return "modules/ka/salesReturnList";
	}

	@RequiresPermissions("ka:salesReturn:view")
	@RequestMapping(value = "form")
	public String form(SalesReturn salesReturn, Model model) {
		model.addAttribute("salesReturn", salesReturn);
		return "modules/ka/salesReturnForm";
	}

	@RequiresPermissions("ka:salesReturn:edit")
	@RequestMapping(value = "save")
	public String save(SalesReturn salesReturn, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, salesReturn)){
			return form(salesReturn, model);
		}
		salesReturnService.save(salesReturn);
		addMessage(redirectAttributes, "保存退货管理成功");
		return "redirect:"+Global.getAdminPath()+"/ka/salesReturn/?repage";
	}
	
	@RequiresPermissions("ka:salesReturn:edit")
	@RequestMapping(value = "delete")
	public String delete(SalesReturn salesReturn, RedirectAttributes redirectAttributes) {
		salesReturnService.delete(salesReturn);
		addMessage(redirectAttributes, "删除退货管理成功");
		return "redirect:"+Global.getAdminPath()+"/ka/salesReturn/?repage";
	}
	
	@RequiresPermissions("ka:salesReturn:view")
	@RequestMapping(value = "logisticsForm")
	public String logisticsForm(SalesReturn salesReturn, Model model) {
		model.addAttribute("salesReturn", salesReturn);
		return "modules/ka/salesReturnLogisticsForm";
	}
	
	@RequiresPermissions("ka:salesReturn:edit")
	@RequestMapping(value = "updateLogistics")
	public String updateLogistics(SalesReturn salesReturn, Model model, RedirectAttributes redirectAttributes) {
		salesReturnService.updateLogistics(salesReturn);
		addMessage(redirectAttributes, "保存物流信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/salesReturn/?repage";
	}
	
	/**
	 * 导出退货数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("ka:salesReturn:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(SalesReturn salesReturn, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "退货数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<SalesReturn> list = salesReturnService.findSalesReturn(salesReturn);
    		new ExportExcel("退货数据", SalesReturn.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出退货数据失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/ka/salesReturn/?repage";
    }

}